package fm.contact.web;

import com.mongodb.BasicDBObject;
import fm.controller.BaseController;
import fm.dao.MongoBaseDao;
import fm.exception.BizException;
import fm.mongo.MongoTable;
import org.apache.solr.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/contact")
public class ContactController extends BaseController {
    @Autowired
    MongoBaseDao mongoBaseDao;

    @ResponseBody
    @RequestMapping(value = "/add")
    public Map addConcat(String phone, String name) {
        Map res = new HashMap();
        try {
            if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(name)) {
                throw new BizException("请务必填写您的真实姓名与手机号码以便客服人员联系您!");
            }

            BasicDBObject object = new BasicDBObject();
            object.put("phone", phone);
            object.put("name", name);
            object.put("latm", new Date());

            mongoBaseDao.insert(object, MongoTable.business_contact);
            this.success(res);
        } catch (BizException ex) {
            LOGGER.info("occur biz error : {} ", ex);
            this.failed(res, ex);
        } catch (Exception e) {
            LOGGER.error("occur error", e);
            this.failed(res, "操作失败，请重试或者联系管理员解决!");
        }
        return res;
    }
}
